﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Configuration

Public Class iLogin

    Private retrievedPassword As String
    Public username As String


    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        'check if textbox are empty.
        If txtUsername.Text = "" Or txtPassword.Text = "" Then
            MessageBox.Show("please enter login details!", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Try
                'retrieve password based on username
                Dim strConn As SqlConnection = New SqlConnection(My.Settings.dbConn.ToString())
                Dim qry As String = "RetrievePassword"
                Dim cmd As New SqlCommand(qry, strConn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add(New SqlParameter("@username", txtUsername.Text))

                strConn.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                While reader.Read()
                    retrievedPassword = reader("password")
                End While
                reader.Close()
                strConn.Close()

                'compare username if pass, show main form else throw error.
                If txtPassword.Text = retrievedPassword Then
                    username = txtUsername.Text
                    txtUsername.Text = ""
                    txtPassword.Text = ""
                    Me.Hide()
                    iMain.Show()
                Else
                    txtPassword.Text = ""
                    MessageBox.Show("authentication failed. please try again.", "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If

            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
        End If

    End Sub

    Private Sub frmMain_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Application.Exit()
    End Sub
End Class
